package com.gitee.shoes.controller;

import cn.hutool.http.HttpUtil;
import com.gitee.shoes.dto.*;
import com.gitee.shoes.enumeration.PlatformEnum;
import com.gitee.shoes.exception.BadRequestException;
import com.gitee.shoes.crawler.bean.ApiProductList;
import com.gitee.shoes.service.ProductService;
import com.gitee.shoes.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author LYH
 */
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class GoodsController {

    private final ProductService productService;

    @PostMapping("/list")
    public Result list(@RequestBody @Validated ProductListQuery productListQuery) {
        PlatformEnum platform = productListQuery.getPlatform();
        if (!platform.isAllowSearch()) {
            throw new BadRequestException("不支持该平台检索");
        }
        Integer current = productListQuery.getCurrent();
        Integer size = productListQuery.getSize();
        if (PlatformEnum.du.equals(platform)) {
            if (current == null || size == null || size <= 0 || current < 1) {
                throw new BadRequestException("current不能为空/size不能为空/current不能小于1/size不能小于0");
            }
        }
        ApiProductList apiProductList = platform.getRequest().productList(productListQuery.getKeyword(), current - 1, size,
                productListQuery.getNextKey(), productListQuery.getOrderMode());
        return Result.ok(apiProductList);
    }

    @PostMapping("/detail")
    public Result detail(@RequestBody ProductDetailQuery productDetailQuery) {
        PlatformEnum platform = productDetailQuery.getPlatform();
        if (!platform.isAllowSearch()) {
            throw new BadRequestException("不支持该平台检索");
        }
        ProductDetailDto productDetailDto = productService.getProductDetailWithRequest(productDetailQuery.getPlatform()
                , productDetailQuery.getPlatformId(), null, null);
        return Result.ok(productDetailDto);
    }

    @PostMapping("/buy-now/{productId}")
    public Result buyNow(@PathVariable Long productId) {
        List<ProductBuyNowDto> productBuyNowDtos = productService.getBuyNowWithRequest(productId);
        return Result.ok(productBuyNowDtos);
    }

    @PostMapping("/sold-record")
    public Result soldRecord(@RequestBody ProductSoldQuery productSoldQuery) {
        ProductSoldListDto productBuyNowDtos = productService.getSoldWithRequest(productSoldQuery);
        return Result.ok(productBuyNowDtos);
    }

    @GetMapping("/test")
    public Result test() {
        log.trace("---trace乱码--");
        log.debug("---debug乱码--");
        log.info("---info乱码--");
        log.warn("---warn乱码--");
        log.error("---error乱码--");
        return Result.ok("ok");
    }

    @GetMapping("/ip")
    public Result ip() {
        String s = HttpUtil.get("https://api.ipify.org/");
        return Result.ok(s);
    }
}
